package com.stoneframe.plugin.webSocketOnline;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

/**
 * 在线管理
 */
public class OnlineChatServer extends WebSocketServer {
	
	public OnlineChatServer(int port) throws UnknownHostException {
		super(new InetSocketAddress(port));
	}

	public OnlineChatServer(InetSocketAddress address) {
		super(address);
	}

	/**
	 * 触发关闭事件
	 */
	@Override
	public void onClose(WebSocket arg0, int arg1, String arg2, boolean arg3) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 触发异常事件
	 */
	@Override
	public void onError(WebSocket arg0, Exception arg1) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 客户端发送消息到服务器时触发事件
	 */
	@Override
	public void onMessage(WebSocket arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 触发连接事件
	 */
	@Override
	public void onOpen(WebSocket arg0, ClientHandshake arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 加入用户
	 */
	public void userjoin(String user, WebSocket conn){
		if(null == OnlineChatServerPool.getWebSocketByUser(user)){				//判断用户是否在其它终端登录
			OnlineChatServerPool.addUser(user,conn);							//向连接池添加当前的连接对象
		}else{
			goOut(conn,"goOut");
		}
	}
	
	/**
	 * 强制某用户下线
	 * @param user
	 */
	public void goOut(String user){
		this.goOut(OnlineChatServerPool.getWebSocketByUser(user),"thegoout");	
	}
	
	
	/**
	 * 强制用户下线
	 * @param conn
	 */
	public void goOut(WebSocket conn,String type){
		JSONObject result = new JSONObject();
		try {
			result.put("type", type);
			result.put("msg", "goOut");
			OnlineChatServerPool.sendMessage(conn,result.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	/**
	 * 用户下线处理
	 * @param user
	 */
	public void userLeave(WebSocket conn){
		OnlineChatServerPool.removeUser(conn);				//在连接池中移除连接
	}

	/**
	 * 切换菜单处理
	 * @param conn
	 */
	public void changeMenu(WebSocket conn){
		if(OnlineChatServerPool.removeUser(conn)){
			JSONObject result = new JSONObject();
			try {
				result.put("type", "changeMenu");
				result.put("msg", "changeMenu");
				OnlineChatServerPool.sendMessage(conn,result.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取在线总数
	 */
	public void getUserCount(WebSocket conn){
		JSONObject result = new JSONObject();
		try {
			result.put("type", "count");
			result.put("msg", OnlineChatServerPool.getUserCount());
			OnlineChatServerPool.sendMessage(conn,result.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
							
	}
	
	/**
	 * 获取在线用户列表
	 */
	public void getUserList(WebSocket conn){
		JSONObject result = new JSONObject();
		try {
			result.put("type", "userlist");
			result.put("list", OnlineChatServerPool.getOnlineUser());
			OnlineChatServerPool.sendMessage(conn,result.toString());	
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	public static void main( String[] args ) throws InterruptedException , IOException {
		WebSocketImpl.DEBUG = false;
		int port = 8887; //端口
		OnlineChatServer s = new OnlineChatServer(port);
		s.start();
		//System.out.println( "服务器的端口" + s.getPort() );
	}

}
